package com.hanson.service;

import com.alibaba.fastjson.JSONObject;
import com.hanson.entity.SpreadInfoBean;
import com.hanson.entity.UserBean;
import com.hanson.mapper.SpreadInfoBeanMapper;
import com.hanson.mapper.UserBeanMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class SpreadInfoService {
    @Autowired
    private SpreadInfoBeanMapper spreadInfoBeanMapper;
    @Autowired
    private UserBeanMapper userBeanMapper;
    @Autowired
    private OrderService orderService;

    public void addSpread(String openId, String parentId) {
        if (openId.equals(parentId)) {
            log.error("【自己不能作为自己的上级 openId:{}】", openId);
            return;
        }
        List<SpreadInfoBean> list = spreadInfoBeanMapper.selectByChildId(openId);
        if (!CollectionUtils.isEmpty(list)) {
            UserBean userBean = userBeanMapper.selectByOpenId(list.get(0).getParentId());
            if (null != userBean) {
                log.error("【当前用户openId:{}已有上级:{}】", openId, userBean.getNickName());
                return;
            } else {
                list.stream().forEach(c -> {
                    spreadInfoBeanMapper.deleteByPrimaryKey(c.getId());
                });
            }
        }
        //计算下级经销商的级别
        List<SpreadInfoBean> parentList = spreadInfoBeanMapper.selectByChildId(parentId);
        int level = 2;
        if (!CollectionUtils.isEmpty(parentList)) {
            Integer parentLevel = parentList.get(0).getLevel();
            parentLevel = parentLevel == null ? 2 : parentLevel;
            level = parentLevel + 1;
        }
        SpreadInfoBean spreadInfoBean = new SpreadInfoBean();
        spreadInfoBean.setChildId(openId);
        spreadInfoBean.setParentId(parentId);
        spreadInfoBean.setLevel(level);
        spreadInfoBean.setCreateDate(new Date());
        spreadInfoBeanMapper.insert(spreadInfoBean);
    }

    public List<SpreadInfoBean> selectChilds(String openId, String search) {
        List<SpreadInfoBean> list = spreadInfoBeanMapper.selectByParentId(openId);
        List<SpreadInfoBean> returnList = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (SpreadInfoBean c : list) {
                UserBean userBean = userBeanMapper.selectByOpenId(c.getChildId());
                if (null == userBean) {
                    continue;
                }
                if (StringUtils.isEmpty(userBean.getUserName())) {
                    userBean.setUserName("");
                }
                if (!StringUtils.isEmpty(search) && (!userBean.getNickName().contains(search)
                        && !userBean.getUserName().contains(search))) {
                    continue;
                }
                c.setChildName(userBean.getNickName());
                c.setChildImage(userBean.getHeadImg());
                JSONObject jsonObject = orderService.countByOpenId(c.getChildId(), null);
                c.setTotalMoneyNum(jsonObject.getString("money"));
                c.setTotalOrderNum(jsonObject.getInteger("num"));
                returnList.add(c);
            }
        }
        return returnList;
    }
}
